var debugMode = true;
// creates an XMLHttpRequest instance
function createXmlHttpRequestObject()
{
  // will store the reference to the XMLHttpRequest object
  var xmlHttp;
  // this should work for all browsers except IE6 and older
  try
  {
    // try to create XMLHttpRequest object
    xmlHttp = new XMLHttpRequest();
  }
  catch(e)
  {
    // assume IE6 or older
    var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
                                    "MSXML2.XMLHTTP.5.0",
                                    "MSXML2.XMLHTTP.4.0",
                                    "MSXML2.XMLHTTP.3.0",
                                    "MSXML2.XMLHTTP",
                                    "Microsoft.XMLHTTP");
    // try every prog id until one works
    for (var i=0; i<XmlHttpVersions.length && !xmlHttp; i++)
    {
      try
      {
        // try to create XMLHttpRequest object
        xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
      }
      catch (e) {}
    }
  }
  // return the created object or display an error message
  if (!xmlHttp)
    alert("Klaida sukuriant XMLHttpRequest objekta.");
  else
    return xmlHttp;
}

// function that displays a new message on the page
function display(element, message)
{
  // obtain a reference to the <div> element on the page
  myElement = document.getElementById(element);
  // display message
  myElement.innerHTML = message;
  if(navigator.userAgent.search("MSIE") >= 0)
    myElement.parentNode.parentNode.style.display = "block";
  else
    myElement.parentNode.parentNode.style.display = "table-row";
  myElement.parentNode.parentNode.previousSibling.className += ' expanded';
}

// function that displays an error message
function displayError(url, element, message)
{
  // display error message, with more technical details if debugMode is true
  display(element, "Nepavyko gauti informacijos iš serverio. Prašome apie šią problemą pranešti administratoriui adresu: pniauman@gmail.com" + (debugMode ? "<br/>" + message : ""));
  // restart sequence
  setTimeout("process(" + url + ", " + element + ");", errorRetryInterval * 1000);
}

function process(url, element)
{
  elem = document.getElementById(element);
  if(elem.innerHTML != "")
  {
    collapse(element);
  }
  else
  {
    expand(url, element);
  }
}

function collapse(element)
{
  elem = document.getElementById(element);
  row = elem.parentNode.parentNode;
  row.style.display = "none";
  row.previousSibling.className = row.previousSibling.className.replace(' expanded', '');
  elem.innerHTML = "";
}

// call server asynchronously 
function expand(url, element)
{
  elem = document.getElementById(element);
  if(elem.innerHTML != "")
  {
    return;
  }
  
  var xmlHttp = createXmlHttpRequestObject();
  // only continue if xmlHttp isn't void
  if (xmlHttp)
  {
    // try to connect to the server
    try
    {
      // make asynchronous HTTP request to retrieve new message
      xmlHttp.onreadystatechange = function()
      {
        // when readyState is 4, we are ready to read the server response
        if (xmlHttp.readyState == 4)
        {
          // continue only if HTTP status is "OK"
          if (xmlHttp.status == 200)
          {
            try
            {
              // do something with the response from the server
              var response = xmlHttp.responseText;
              // server error?
              if (response.indexOf("ERRNO") >= 0
                  || response.indexOf("error") >= 0
                  || response.length == 0)
                throw(response.length == 0 ? "Serverio klaida." : response);
              // don't change if it is the same
              //display the message
              display(element, response);
            }
            catch(e)
            {
              // display error message
              displayError(url, element, e.toString());
            }
          }
          else
          {
            // display error message
            displayError(url, element, xmlHttp.statusText);
          }
        }
        return false;
      }
      xmlHttp.open("GET", url, true);
      xmlHttp.send(null);
    }
    catch(e)
    {
      displayError(url, element, e.toString());
    }
    return true;
  }
}

function expandAll(ids)
{
  id = document.getElementById(ids).value.split(",");
  for (i=1; i < id.length; i++)
  {
    expand(id[0]+'/'+id[i], 'projectWorkhours_'+id[i]+'');
  }
}

function collapseAll(ids)
{
  id = document.getElementById(ids).value.split(",");
  for (i=1; i < id.length; i++)
  {
    collapse('projectWorkhours_'+id[i]+'');
  }
}
